package com.wiz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.common.Constant;
import com.wiz.mapper.AdminLoginInfoMapper;
import com.wiz.mapper.GradeConfigMapper;
import com.wiz.mapper.HQBaseInfoMapper;
import com.wiz.mapper.MemberGradeConfigMapper;
import com.wiz.mapper.PaidTypeConfigMapper;
import com.wiz.mapper.PaidTypeMapper;
import com.wiz.mapper.StationMapper;
import com.wiz.service.HeadquartersService;
import com.wiz.to.AdminLoginInfoTO;
import com.wiz.to.GradeConfigTO;
import com.wiz.to.HQBaseInfoTO;
import com.wiz.to.MemberGradeConfigTO;
import com.wiz.to.PaidTypeConfigTO;
import com.wiz.to.PaidTypeTO;
import com.wiz.to.StationTO;
import com.wiz.util.FileUtil;
import com.wiz.util.KeyUtil;
import com.wiz.vo.criteria.HqSearchCriteriaTO;
//@Transactional
@Service("headquartersService")
public class HeadquartersServiceImpl implements HeadquartersService {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private HQBaseInfoMapper hqBaseInfoMapper;
	@Resource
	private StationMapper stationMapper;
	@Resource
	private PaidTypeMapper paidTypeMapper;
	@Resource
	private PaidTypeConfigMapper paidTypeConfigMapper;
	@Resource
	private AdminLoginInfoMapper adminLoginInfoMapper;
	@Resource
	private GradeConfigMapper gradeConfigMapper;
	@Resource
	private MemberGradeConfigMapper memberGradeConfigMapper;

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void add(HQBaseInfoTO hqBaseInfoTO,MultipartFile file)throws Exception {
//		int random = Constant.STATION_RANDOM;
		String hqId = hqBaseInfoMapper.getMaxPhId();
		//测试用
		if(hqId == null || hqId.equals("")){
			hqId = String.valueOf(Constant.HQ_ID_RANDOM+1);
		}else{
			hqId = String.valueOf(Integer.valueOf(hqId)+1);
		}
		
		//保存图片
		String logoUrl = null;
		if(file != null&&file.getSize()>0){
			logoUrl = FileUtil.uploadFile(file,Constant.HQ_DIRECTORY,hqId,0);
		}
		
		
		
		hqBaseInfoTO.setHqId(hqId);
		hqBaseInfoTO.setLogoUrl(logoUrl);
		
		//创建一个类型为总部的油站
		StationTO stationTO = new StationTO(hqBaseInfoTO);
		stationTO.setActivationCode(KeyUtil.getActivationCode());
		hqBaseInfoMapper.add(hqBaseInfoTO);
		stationMapper.addStation(stationTO);
		stationMapper.addBaseInfo(stationTO);
		hqBaseInfoMapper.addWeiXinConfig(hqBaseInfoTO.getHqId(), hqBaseInfoTO.getHqName(),hqBaseInfoTO.getCreatedTime());
		hqBaseInfoMapper.addIncrementiId(hqId, 100000000+(int)(Math.random()*99999999), 100000+(int)(Math.random()*99999));
		
		
		Map<String,Object>map = new HashMap<String, Object>();
		map.put("companyId",hqBaseInfoTO.getHqId()+"000000" );
		map.put("hq",hqBaseInfoTO);
		hqBaseInfoMapper.addCompany(map);
		
		AdminLoginInfoTO adminLoginInfoTO = new AdminLoginInfoTO(hqBaseInfoTO);
		adminLoginInfoMapper.add(adminLoginInfoTO);
		//创建默认支付方式
		List<PaidTypeConfigTO>paidTypeConfigs = this.addPaidTypeConfig(hqBaseInfoTO);
		paidTypeConfigMapper.addBatch(paidTypeConfigs);
		//总部会员等级配置信息
		List<MemberGradeConfigTO> memberGradeConfigs = this.addmemberGradeConfig(hqBaseInfoTO);
		memberGradeConfigMapper.addBatch(memberGradeConfigs);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
	public void update(HQBaseInfoTO hqBaseInfoTO,MultipartFile file) throws Exception {
		String logoUrl = null;
		if(file.getSize()>0 ){
			logoUrl = FileUtil.uploadFile(file,Constant.HQ_DIRECTORY,hqBaseInfoTO.getHqId(),0);
			hqBaseInfoTO.setLogoUrl(logoUrl);
		}
		
		//修改一个类型为总部的油站
		StationTO stationTO = new StationTO(hqBaseInfoTO);
		
		hqBaseInfoMapper.update(hqBaseInfoTO);
		stationMapper.updateStation(stationTO);
		stationMapper.updateBaseInfo(stationTO);
		
	}

	@Override
	public List<HQBaseInfoTO> gethqsByCriteriaTO(
			HqSearchCriteriaTO hqSearchCriteriaTO) {
		return hqBaseInfoMapper.gethqsByCriteriaTO(hqSearchCriteriaTO);
	}

	@Override
	public int gethqsCntByCriteriaTO(HqSearchCriteriaTO hqSearchCriteriaTO) {
		return hqBaseInfoMapper.gethqsCntByCriteriaTO(hqSearchCriteriaTO);
	}

	@Override
	public HQBaseInfoTO gethqsById(String hqId) throws Exception {
		List<HQBaseInfoTO>list = hqBaseInfoMapper.gethqsById(hqId);
		if(list != null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	@Transactional( propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void remove(String hqId) throws Exception {
		List<StationTO> list = stationMapper.getStationByHq(hqId);
		if(list!= null &&list.size()>0){
			throw new Exception("总部下有油站，不能删除！");
		}
		stationMapper.removeBaseInfoByHq(Integer.valueOf(hqId));
		stationMapper.removeStationByHq(Integer.valueOf(hqId));
		 hqBaseInfoMapper.remove(hqId);
	}
	@Override
	public List<HQBaseInfoTO> getListHq()throws Exception {
		return hqBaseInfoMapper.getListHq();
	}
	/**创建默认支付方式*/
	public List<PaidTypeConfigTO> addPaidTypeConfig(HQBaseInfoTO hqBaseInfoTO){
		List<PaidTypeConfigTO> configList = new ArrayList<PaidTypeConfigTO>();
		List<PaidTypeTO>paidTypeList =  paidTypeMapper.queryAll();
		PaidTypeTO paidTypeTO = null;
		
		for (int i = 0; i < paidTypeList.size(); i++) {
			paidTypeTO = paidTypeList.get(i);
			configList.add(new PaidTypeConfigTO(paidTypeTO.getId(), paidTypeTO.getName(),
					hqBaseInfoTO.getHqId(), hqBaseInfoTO.getHqName(), hqBaseInfoTO.getCreatedTime(), hqBaseInfoTO.getCreatedTime()));
		}
		return configList;
		
	}
	public List<MemberGradeConfigTO> addmemberGradeConfig(HQBaseInfoTO hqBaseInfoTO){
		List<MemberGradeConfigTO> configList = new ArrayList<MemberGradeConfigTO>();
		List<GradeConfigTO> list =  gradeConfigMapper.query();
		GradeConfigTO gradeConfigTO = null;
		int isDefault = 0;
		for (int i = 0; i < list.size(); i++) {
			gradeConfigTO = list.get(i);
			if(gradeConfigTO.getId() == 6){
				isDefault = 1;
			}else{
				isDefault = 0;
			}
			configList.add(new MemberGradeConfigTO(0, gradeConfigTO.getName(), hqBaseInfoTO.getHqId(),
					gradeConfigTO.getPoint(), gradeConfigTO.getSaleAmount(), hqBaseInfoTO.getCreatedTime(), isDefault, hqBaseInfoTO.getUpdateTime()));
		}
		return configList;
	}

	

	
}